package com.wh.wisdomsite.xbox.utils.pay.yeepay;

import com.wh.wisdomsite.xbox.utils.random.RandomTool;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


/**
 * AES加密解密工具2
 * @Package com.wh.wisdomsite.xbox.utils.encrypt.aes
 * @author 谢泽鹏
 * @date 2014年10月8日 下午4:02:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 * @version V1.0
 */
public class AES {


    private static final Log logger = LogFactory.getLog(AES.class);


    public static final String CHAR_ENCODING = "UTF-8";
    public static final String AES_ALGORITHM = "AES/ECB/PKCS5Padding";
    public static final String RSA_ALGORITHM = "RSA/ECB/PKCS1Padding";


    /**
     * 加密
     * @param data 加密内容
     * @param key  加密密码(可以用16个字母和数字组成)
     * @return
     */
    public static String encryptToBase64(String data, String key) {
        //加密结果
        String encrypt_src = null;
        try {
            if(null == data){
                logger.error("encryptToBase64->>加密内容data为空！");
                return null;
            }
            if(null == key){
                logger.error("encryptToBase64->>加密密码key为空！");
                return null;
            }
            if(16 != key.length()){
                logger.error("encryptToBase64->>加密密码key秘钥错误！" + key.length());
                return null;
            }
            logger.info("encryptToBase64->>加密内容data：" + data);
            logger.info("encryptToBase64->>加密密码key：" + key);
            byte[] valueByte = encrypt(data.getBytes(CHAR_ENCODING), key.getBytes(CHAR_ENCODING));
            encrypt_src = new String(Base64.encode(valueByte));
            logger.info("encryptToBase64->>加密值：" + encrypt_src);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("encryptToBase64->>加密Exception：" + e.getMessage());
            throw new RuntimeException("数据加密失败");
        }
            return encrypt_src;
    }





    /**
     * 解密
     * @param data 解密内容
     * @param key  解密密码(可以用16个字母和数字组成)
     * @return
     */
    public static String decryptFromBase64(String data, String key){
        //解密结果
        String decrypt_src = null;
        try {
            if(null == data){
                logger.error("decryptFromBase64->>解密内容data为空！");
                return null;
            }
            if(null == key){
                logger.error("decryptFromBase64->>解密密码key为空！");
                return null;
            }
            if(16 != key.length()){
                logger.error("decryptFromBase64->>解密密码key秘钥错误！" + key.length());
                return null;
            }
            logger.info("decryptFromBase64->>解密内容data：" + data);
            logger.info("decryptFromBase64->>解密密码key：" + key);
            byte[] originalData = Base64.decode(data.getBytes());
            byte[] valueByte = decrypt(originalData, key.getBytes(CHAR_ENCODING));
            decrypt_src = new String(valueByte, CHAR_ENCODING);
            logger.info("decryptFromBase64->>解密值：" + decrypt_src);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("decryptFromBase64->>解密Exception：" + e.getMessage());
            throw new RuntimeException("数据解密失败");
        }
            return decrypt_src;
    }


    /**
     * 加密
     * @param data 加密内容
     * @param key  加密密码(可以用16个字母和数字组成)
     * @return
     */
    public static String encryptWithKeyBase64(String data, String key){
        //加密结果
        String encrypt_src = null;
        try {
            if(null == data){
                logger.error("encryptWithKeyBase64->>加密内容data为空！");
                return null;
            }
            if(null == key){
                logger.error("encryptWithKeyBase64->>加密密码key为空！");
                return null;
            }
            logger.info("encryptWithKeyBase64->>加密内容data：" + data);
            logger.info("encryptWithKeyBase64->>加密密码key：" + key);
            byte[] valueByte = encrypt(data.getBytes(CHAR_ENCODING), Base64.decode(key.getBytes()));
            encrypt_src = new String(Base64.encode(valueByte));
            logger.info("encryptWithKeyBase64->>加密值：" + encrypt_src);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("encryptWithKeyBase64->>加密Exception：" + e.getMessage());
            throw new RuntimeException("数据加密失败");
        }
        return encrypt_src;
    }

    /**
     * 解密
     * @param data 解密内容
     * @param key  解密密码(可以用16个字母和数字组成)
     * @return
     */
	public static String decryptWithKeyBase64(String data, String key){
        //解密结果
        String decrypt_src = null;
        try {
            if(null == data){
                logger.error("decryptWithKeyBase64->>解密内容data为空！");
                return null;
            }
            if(null == key){
                logger.error("decryptWithKeyBase64->>解密密码key为空！");
                return null;
            }
            logger.info("decryptWithKeyBase64->>解密内容data：" + data);
            logger.info("decryptWithKeyBase64->>解密密码key：" + key);
			byte[] originalData = Base64.decode(data.getBytes());
			byte[] valueByte = decrypt(originalData, Base64.decode(key.getBytes()));
            decrypt_src = new String(valueByte, CHAR_ENCODING);
            logger.info("decryptFromBase64->>加密值：" + decrypt_src);
		} catch (Exception e) {
            e.printStackTrace();
            logger.error("decryptWithKeyBase64->>解密Exception：" + e.getMessage());
            throw new RuntimeException("数据解密失败");
		}
            return decrypt_src;
	}


    /**
     * 加密操作
     * @param data 加密内容
     * @param key  加密密码
     * @return
     */
    public static byte[] encrypt(byte[] data, byte[] key) throws Exception{
        byte[] result = null;
        try {
            SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec seckey = new SecretKeySpec(enCodeFormat,"AES");
            Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, seckey);
            result = cipher.doFinal(data);
        } catch (Exception e){
            e.printStackTrace();
            logger.error("encrypt->>加密Exception：" + e.getMessage());
            throw new RuntimeException("数据加密异常");
        }
            return result;
    }



    /**
     * 解密操作
     * @param data 待解密内容
     * @param key 解密密钥
     * @return
     */
    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        byte[] result = null;
        try {
            SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec seckey = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, seckey);
            result = cipher.doFinal(data);
        } catch (Exception e){
            e.printStackTrace();
            logger.error("decrypt->>解密Exception：" + e.getMessage());
            throw new RuntimeException("数据解密异常");
        }
            return result;
    }


    public static void main(String[] args) throws Exception {

       System.out.println("############加减密第1测试开始###########");
       String key = RandomTool.getNumberLetterAll(16);
       String data = "互联网第一品牌23864324##@@";
       String encrypt_src = encryptToBase64(data, key);
       decryptFromBase64(encrypt_src, key);
       System.out.println("############加减密第1测试结束###########");

       System.out.println("############加减密第2测试开始###########");
       key = RandomTool.getNumberLetterAll(21);
       String encrypt_src1 = encryptWithKeyBase64(data, key);
       decryptWithKeyBase64(encrypt_src1, key);
       System.out.println("############加减密第2测试结束###########");

    }



}
